
import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy {

    public static void main(String[] args) throws Exception{
        Singera target = new Singera();
        ISinger proxy = (ISinger) Proxy.newProxyInstance(
                Thread.currentThread().getContextClassLoader(),
                target.getClass().getInterfaces(),
                (proxy1, method, args1) -> {
                    System.out.println("向观众问好");//标黄
                    //执行目标对象方法
                    Object returnValue = method.invoke(target, args1);
                    System.out.println("谢谢大家");//标黄
                    return returnValue;
                });
        proxy.sing();
        System.out.println(proxy);
//        byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{Singer.class});
//        FileOutputStream fileOutputStream = new FileOutputStream("Proxy0.class");
//        fileOutputStream.write(bytes);
//        fileOutputStream.close();

    }
}
class Singera implements ISinger {
    @Override
    public void sing() {
        System.err.println("唱一首歌");
    }

    public static void print(){
        System.out.println("...invoke print...");
    }

}
